package com.iris.smartclass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iris.smartclass.common.ErrorCode;
import com.iris.smartclass.constant.CommonConstant;
import com.iris.smartclass.exception.BusinessException;
import com.iris.smartclass.mapper.DutyMapper;
import com.iris.smartclass.mapper.StudentMapper;
import com.iris.smartclass.model.dto.duty.DutyQueryRequest;
import com.iris.smartclass.model.dto.duty.DutyUpdateRequest;
import com.iris.smartclass.model.entity.Duty;
import com.iris.smartclass.model.entity.Student;
import com.iris.smartclass.model.entity.User;
import com.iris.smartclass.model.vo.DutyVO;
import com.iris.smartclass.service.DutyService;
import com.iris.smartclass.service.UserService;
import com.iris.smartclass.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
* @author 孙鸣
* @description 针对表【duty(值日表)】的数据库操作Service实现
* @createDate 2024-12-31 09:20:28
*/
@Service
public class DutyServiceImpl extends ServiceImpl<DutyMapper, Duty>
    implements DutyService {
    @Resource
    private StudentMapper studentMapper;

    @Resource
    private DutyMapper dutyMapper;

    @Resource
    private UserService userService;

    @Override
    public DutyVO getDutyVO(long stId) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",stId);
        Student student = studentMapper.selectOne(queryWrapper);
        if (student == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        String name = student.getName();
        DutyVO dutyVO = new DutyVO();
        dutyVO.setName(name);
        return dutyVO;
    }

    @Override
    public List<DutyVO> getDutyVOList(int day,HttpServletRequest request) {
        List<DutyVO> dutyVOList = new ArrayList<>();
        User loginUser = userService.getLoginUser(request);
        Long uuid = loginUser.getId();
        QueryWrapper<Duty> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("day",day);
        queryWrapper.eq("uuid",uuid);
        List<Duty> dutyList = dutyMapper.selectList(queryWrapper);
        for (Duty duty : dutyList) {
            DutyVO dutyVO = getDutyVO(duty.getStId());
            BeanUtils.copyProperties(duty,dutyVO);
            dutyVOList.add(dutyVO);
        }
        return dutyVOList;
    }

    @Override
    public QueryWrapper<Duty> getQueryWrapper(DutyQueryRequest dutyQueryRequest, HttpServletRequest request) {
        if ( dutyQueryRequest== null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        User loginUser = userService.getLoginUser(request);
        Long uuid = loginUser.getId();
        Long stId = dutyQueryRequest.getStId();
        Integer day = dutyQueryRequest.getDay();
        String sortField = dutyQueryRequest.getSortField();
        String sortOrder = dutyQueryRequest.getSortOrder();
        QueryWrapper<Duty> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(uuid != null, "uuid", uuid);
        queryWrapper.eq(day != null, "day", day);
        queryWrapper.eq(stId != null, "stId", stId);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    @Override
    public DutyVO getDutyVOByID(Long stId,Long uuid) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",stId);
        Student student = studentMapper.selectOne(queryWrapper);
        QueryWrapper<Duty> dutyQueryWrapper = new QueryWrapper<>();
        dutyQueryWrapper.eq("stId",stId);
        dutyQueryWrapper.eq("uuid",uuid);
        Duty duty = dutyMapper.selectOne(dutyQueryWrapper);
        if (student == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        String name = student.getName();
        DutyVO dutyVO = new DutyVO();
        dutyVO.setName(name);
        BeanUtils.copyProperties(duty,dutyVO);
        return dutyVO;
    }

    @Override
    public boolean updateDutyByName(DutyUpdateRequest dutyUpdateRequest) {
        if (dutyUpdateRequest == null || dutyUpdateRequest.getName() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = dutyUpdateRequest.getName();
        Long id = studentMapper.selectOne(new QueryWrapper<Student>().eq("name", name)).getId();
        Duty duty = new Duty();
        duty.setStId(id);
        BeanUtils.copyProperties(dutyUpdateRequest, duty);
        int i = dutyMapper.update(duty, new QueryWrapper<Duty>().eq("stId", id));
        return i>0;
    }
}




